﻿<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>数据字典</title>
	<jsp:include page="/zheng-admin-plugins/plugins/inc/head.jsp" flush="true"/>
</head>
<body style="font-size:14px;">
<div id="main">
	<div id="toolbar">
		<input id="fatherCode" type="hidden" name="fatherCode" value="sys">
		<input id="fatherDesc" type="hidden" name="fatherDesc" value="系统">
		<shiro:hasPermission name="upms:organization:create"><a class="waves-effect waves-button" href="javascript:;" onclick="createAction()"><i class="zmdi zmdi-plus"></i> 新增字典</a></shiro:hasPermission>
		<shiro:hasPermission name="upms:organization:update"><a class="waves-effect waves-button" href="javascript:;" onclick="updateAction()"><i class="zmdi zmdi-edit"></i> 编辑字典</a></shiro:hasPermission>
		<shiro:hasPermission name="upms:organization:delete"><a class="waves-effect waves-button" href="javascript:;" onclick="deleteAction()"><i class="zmdi zmdi-close"></i> 删除字典</a></shiro:hasPermission>
		<%--<div class="dropdown">--%>
			<%--<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">--%>
				<%--查询--%>
				<%--<span class="caret"></span>--%>
			<%--</button>--%>
			<%--<ul id="dropdownMenu" class="dropdown-menu" aria-labelledby="dropdownMenu1">--%>
			<%--</ul>--%>
		<%--</div>--%>
	</div>
	<table id="table"></table>
</div>
<jsp:include page="/zheng-admin-plugins/plugins/inc/footer.jsp" flush="true"/>
<script>
var $table = $('#table');
$(function() {
	//查询父编码为sys的所有数据
	selectDictionaryByDescriptionIsSys();
});
//查询父编码为sys的所有数据
function selectDictionaryByDescriptionIsSys(){
    var desc="sys";
    getTable(desc);
    getDropDown();
}
//更新下拉菜单
function getDropDown(){
    $.ajax({
        type: 'get',
        url: '${basePath}/manage/dictionary/selectNoPagination/' + "sys",
        success: function(result) {
            if (result.total>0){
				buildDropdownMenu(result.total,result.rows);
			}else{

			}
        }
    });
}
//构建DropdownMenu
function buildDropdownMenu(i,data){
    for (var j=0;j<i;j++){
        var code=data[j].code;
        var desc=data[j].description;
        if (j==0){
            var html="<li><a onclick='refreshTable("+'"'+code+'"'+','+'"'+desc+'"'+");' href='#'>"+desc+"</a></li>";
		}else{
            html = html + "<li><a onclick='refreshTable("+'"'+code+'"'+','+'"'+desc+'"'+");' href='#'>" + desc + "</a></li>";
		}
	}
	$(".dropdown-menu").html(html);

}
//点击列表查询字典
function refreshTable(code,desc){
    $("#fatherCode").val(code);
    $("#fatherDesc").val(desc);
    $table.bootstrapTable('refresh', {url: '${basePath}/manage/dictionary/select/'+code});
}
//查询并给dataGrid赋值
function getTable(desc){
    var getTable = $('#table');
    getTable.bootstrapTable({
        url: '${basePath}/manage/dictionary/select/'+desc,
        //height: getHeight(),
        striped: true,
//        search: true,
        showRefresh: true,
        showColumns: true,
        minimumCountColumns: 2,
        clickToSelect: true,
        detailView: false,
        detailFormatter: 'detailFormatter',
        pagination: true,
        paginationLoop: false,
        pageSize:50,
        pageList:[50],
        sidePagination: 'server',
        silentSort: false,
        smartDisplay: false,
        escape: true,
        searchOnEnterKey: true,
        idField: 'pkId',
        sortName: 'pkId',
        sortOrder: 'asc',
        maintainSelected: true,
        toolbar: '#toolbar',
        columns: [
            {field: 'ck', checkbox: true},
            {field: 'pkId', title: '编号', sortable: true, align: 'center',visible:false},
            {field: 'code', title: '编码'},
            {field: 'description', title: '描述'},
            {field: 'remarks', title: '备注'},
            {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
        ],
        onLoadSuccess:function(data){
            //无数据隐藏分页
            if(data && data.total == 0){
                $('.fixed-table-pagination').hide()
            }
        }
    });
}
// 格式化操作按钮
function actionFormatter(value, row, index) {
    return [
		'<a class="update" href="javascript:;" onclick="updateAction(\'' + row.pkId + '\')" data-toggle="tooltip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>　',
		'<a class="delete" href="javascript:;" onclick="deleteAction(\'' + row.pkId + '\')" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>'
    ].join('');
}

//查询
var selectDialog;
function selectAction() {
    selectDialog = $.dialog({
        animationSpeed: 300,
        title: '新增字典',
        content: 'url:${basePath}/manage/dictionary/select',
        onContentReady: function () {
            initMaterialInput();
        }
    });
}
// 新增
var createDialog;
function createAction() {
	createDialog = $.dialog({
		animationSpeed: 300,
		title: '新增字典',
		content: 'url:${basePath}/manage/dictionary/create/'+$("#fatherCode").val()+"&"+$("#fatherDesc").val(),
		onContentReady: function () {
			initMaterialInput();
		}
	});
    getDropDown();
}
// 编辑
var updateDialog;
function updateAction(id) {
	var rows = $table.bootstrapTable('getSelections');
    if(id==null||typeof(id)=="undefined"){
        if (rows.length != 1) {
            $.confirm({
                title: false,
                content: '请选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else{
            updateDialog = $.dialog({
                animationSpeed: 300,
                title: '编辑字典',
                content: 'url:${basePath}/manage/dictionary/update/' + rows[0].pkId,
                onContentReady: function () {
                    initMaterialInput();
                }
            });
        }
	}else{
        updateDialog = $.dialog({
            animationSpeed: 300,
            title: '编辑字典',
            content: 'url:${basePath}/manage/dictionary/update/' + id,
            onContentReady: function () {
                initMaterialInput();
            }
        });
	}
}
// 删除
var deleteDialog;
function deleteAction(id) {
	var rows = $table.bootstrapTable('getSelections');
    if(id==null||typeof(id)=="undefined"){
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            deleteDialog = $.confirm({
                type: 'red',
                animationSpeed: 300,
                title: false,
                content: '确认删除该字典吗？',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'waves-effect waves-button',
                        action: function () {
                            var ids = new Array();
                            for (var i in rows) {
                                ids.push(rows[i].pkId);
                            }
                            $.ajax({
                                type: 'get',
                                url: '${basePath}/manage/dictionary/delete/' + ids.join("-"),
                                success: function(result) {
                                    if (result.code != 1) {
                                        if (result.data instanceof Array) {
                                            $.each(result.data, function(index, value) {
                                                $.confirm({
                                                    theme: 'dark',
                                                    animation: 'rotateX',
                                                    closeAnimation: 'rotateX',
                                                    title: false,
                                                    content: value.errorMsg,
                                                    buttons: {
                                                        confirm: {
                                                            text: '确认',
                                                            btnClass: 'waves-effect waves-button waves-light'
                                                        }
                                                    }
                                                });
                                            });
                                        } else {
                                            $.confirm({
                                                theme: 'dark',
                                                animation: 'rotateX',
                                                closeAnimation: 'rotateX',
                                                title: false,
                                                content: result.data.errorMsg,
                                                buttons: {
                                                    confirm: {
                                                        text: '确认',
                                                        btnClass: 'waves-effect waves-button waves-light'
                                                    }
                                                }
                                            });
                                        }
                                    } else {
                                        deleteDialog.close();
                                        $table.bootstrapTable('refresh');
                                    }
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    $.confirm({
                                        theme: 'dark',
                                        animation: 'rotateX',
                                        closeAnimation: 'rotateX',
                                        title: false,
                                        content: textStatus,
                                        buttons: {
                                            confirm: {
                                                text: '确认',
                                                btnClass: 'waves-effect waves-button waves-light'
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    },
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        }
	}else{
        deleteDialog = $.confirm({
            type: 'red',
            animationSpeed: 300,
            title: false,
            content: '确认删除该字典吗？',
            buttons: {
                confirm: {
                    text: '确认',
                    btnClass: 'waves-effect waves-button',
                    action: function () {
                        var ids = new Array();
                        for (var i in rows) {
                            ids.push(rows[i].pkId);
                        }
                        $.ajax({
                            type: 'get',
                            url: '${basePath}/manage/dictionary/delete/' + id,
                            success: function(result) {
                                if (result.code != 1) {
                                    if (result.data instanceof Array) {
                                        $.each(result.data, function(index, value) {
                                            $.confirm({
                                                theme: 'dark',
                                                animation: 'rotateX',
                                                closeAnimation: 'rotateX',
                                                title: false,
                                                content: value.errorMsg,
                                                buttons: {
                                                    confirm: {
                                                        text: '确认',
                                                        btnClass: 'waves-effect waves-button waves-light'
                                                    }
                                                }
                                            });
                                        });
                                    } else {
                                        $.confirm({
                                            theme: 'dark',
                                            animation: 'rotateX',
                                            closeAnimation: 'rotateX',
                                            title: false,
                                            content: result.data.errorMsg,
                                            buttons: {
                                                confirm: {
                                                    text: '确认',
                                                    btnClass: 'waves-effect waves-button waves-light'
                                                }
                                            }
                                        });
                                    }
                                } else {
                                    deleteDialog.close();
                                    $table.bootstrapTable('refresh');
                                }
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                $.confirm({
                                    theme: 'dark',
                                    animation: 'rotateX',
                                    closeAnimation: 'rotateX',
                                    title: false,
                                    content: textStatus,
                                    buttons: {
                                        confirm: {
                                            text: '确认',
                                            btnClass: 'waves-effect waves-button waves-light'
                                        }
                                    }
                                });
                            }
                        });
                    }
                },
                cancel: {
                    text: '取消',
                    btnClass: 'waves-effect waves-button'
                }
            }
        });
	}

}
</script>
</body>
</html>